package com.villaday.idf.util;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.villaday.idf.AppContext;
import com.villaday.idf.activity.BieShuSearchResultActivity;
import com.villaday.idf.activity.BourbonListActivity;
import com.villaday.idf.activity.CashWithdrawalActivity;
import com.villaday.idf.activity.H5AutoTitleActivity;
import com.villaday.idf.activity.HomeBatchActivity;
import com.villaday.idf.activity.Home_temaiSearchActivity;
import com.villaday.idf.activity.HouseDetailsActivityFor3;
import com.villaday.idf.activity.InviteDownloadActivity;
import com.villaday.idf.activity.InviteLiBaoActivity;
import com.villaday.idf.activity.JDMSBSActivity;
import com.villaday.idf.activity.MinsuSearchResultActivity;
import com.villaday.idf.activity.MyOrderActivity;
import com.villaday.idf.activity.SVIPFreeActivity;
import com.villaday.idf.activity.SearchBournActivity;
import com.villaday.idf.activity.SearchMinSu2VActivity;
import com.villaday.idf.activity.SleepActivity;
import com.villaday.idf.activity3.JiaFanActivity;
import com.villaday.idf.fragment.OrdinaryVipFragment;

public class JumpUtils {
    //跳转类型
    public static void jump(Context context, int JumpType, String url, OrdinaryVipFragment.Jumps jumps, OrdinaryVipFragment.OnViplPageListener VPListener,String MiniAppId,int MiniProgramType){
        if (JumpType == 1) {//jumpType": "integer,跳转类型，1 APP页面跳转，
            if (url.contains("page/house/detail?id=")) {//id为房源ID，如有其它参数原样带入查询
                String[] split = url.split("=");

                Intent intent1 = new Intent(context, HouseDetailsActivityFor3.class);
                intent1.putExtra("houseId", split[1]);
                intent1.putExtra("persellId", split[1]);
//                    intent.putExtra("type", data.get(position).getType());
                context.startActivity(intent1);
            } else if (url.contains("page/village/detail?id=")) {//id为目的地ID，如有其它参数原样带入查询
//"page/village/detail";  目的地详情页

                String[] split = url.split("=");

                Intent intent1 = new Intent(context, BourbonListActivity.class);

                intent1.putExtra("villageId", split[1]);
                intent1.putExtra("city", "");
                intent1.putExtra("area", "");
                context.startActivity(intent1);
            } else if (url.contains("page/presell/detail?id=")) {//id为预售ID，如有其它参数原样带入查询
                String[] split = url.split("=");

                Intent intent1 = new Intent(context, HouseDetailsActivityFor3.class);
                intent1.putExtra("houseId", split[1]);
                intent1.putExtra("persellId", split[1]);
                intent1.putExtra("presellProduct", true);
//              intent.putExtra("type", data.get(position).getType());
                context.startActivity(intent1);
            } else if (url.contains("page/village/list")) {//找房源-别墅
                //jumps.orderJump(1, 0);

                Intent intent = new Intent(context, JDMSBSActivity.class);
                intent.putExtra("type",0);

                context.startActivity(intent);

            } else if (url.contains("page/house/list")) {//找房源-民宿
               /* if (jumps != null) {
                    jumps.orderJump(1, 1);
                }*/
                Intent intent = new Intent(context, JDMSBSActivity.class);
                intent.putExtra("type",0);

                context.startActivity(intent);
            } else if (url.contains("page/member")) {//我的

                if (jumps != null) {
                    jumps.orderJump(4, -1);
                }

            } else if (url.contains("page/presell/search")) {//预售搜索
                context.startActivity(new Intent(context, Home_temaiSearchActivity.class));

            } else if (url.contains("page/home")) {//首页
                if (VPListener != null) {
                    VPListener.vipJump();
                }

            } else if ("page/search".equals(url)) {
                //"page/search";  房源搜索索引页

                Intent intent1 = new Intent(context, SearchBournActivity.class);
                intent1.putExtra("cityId", AppContext.currentCityidCurrent);
                context.startActivity(intent1);
            } else if ("page/search/result".equals(url)) { //"page/search/result";  房源搜索结果页
                Intent intent = new Intent(context, BieShuSearchResultActivity.class);
                intent.putExtra("isBieShu", true);
            } else if ("page/member/order".equals(url)) {
                //"page/member/order";  我的订单
                Intent intent1 = new Intent(context, MyOrderActivity.class);
                context.startActivity(intent1);
            } else if ("page/vip".equals(url)) {
                //"page/vip";  VIP

            } else if ("page/vip/share".equals(url)) {
                //"page/vip/share";  vip邀请好友分享页
                if ("1".equals(SharedPreferencesUtil.getUserInfo(context).getBody().getMember().getType())) {//vip
                    context.startActivity(new Intent(context, InviteLiBaoActivity.class));

                } else if ("2".equals(SharedPreferencesUtil.getUserInfo(context).getBody().getMember().getType())) {//svip
                    context.startActivity(new Intent(context, SVIPFreeActivity.class));

                }
            } else if ("page/vip/draw".equals(url)) {
                //"page/vip/draw";  vip提款页
                context.startActivity(new Intent(context, CashWithdrawalActivity.class));

            } else if ("page/search?type=1".equals(url)) {//民宿搜索页
                Intent intent = new Intent(context, SearchMinSu2VActivity.class);
                context.startActivity(intent);
            } else if ("page/search/result?type=1".equals(url)) {//民宿搜索结果页
                Intent intent = new Intent(context, MinsuSearchResultActivity.class);
                intent.putExtra("businessDistrictId", matchUrl(url, "businessDistrictId"));
                intent.putExtra("tag", matchUrl(url, "tag"));
                context.startActivity(intent);

            } else if ("page/vip/download".equals(url)) {//VIP邀请下载页
                Intent intent = new Intent(context, InviteDownloadActivity.class);
                context.startActivity(intent);
            }else if ("page/freeSleep/detail".equals(url)){//试睡活动页
                context.startActivity(new Intent(context, SleepActivity.class));

            }else if ("page/addBrokerage/detail".equals(url)){//加返活动页
                context.startActivity(new Intent(context, JiaFanActivity.class));

            }else if ("page/material".equals(url)){//批量转发
                context.startActivity(new Intent(context, HomeBatchActivity.class));

            }else if (url.contains("page/vip/invite/friend")){//免费邀请

                String[] split = url.split("\\?");
                if (split.length==2){
                    String[] split1 = split[1].split("&");
                    if (split1.length==2){
                        String upgrade=null;
                        String IdfActivityId=null;
                        String[] split2 = split1[0].split("=");
                        String[] split21 = split1[1].split("=");

                        if (split2.length==2){
                            if ("upgrade".equals(split2[0])){
                                upgrade=split2[1];
                            }else if ("idfActivityId".equals(split2[0])){
                                IdfActivityId=split2[1];
                            }
                            if ("upgrade".equals(split21[0])){
                                upgrade=split21[1];
                            }else if ("idfActivityId".equals(split21[0])){
                                IdfActivityId=split21[1];
                            }

                            if (!TextUtils.isEmpty(upgrade)&&!TextUtils.isEmpty(IdfActivityId)){
                                Intent intent = new Intent(context, SVIPFreeActivity.class);
                                intent.putExtra("upgrade",upgrade);
                                intent.putExtra("IdfActivityId",IdfActivityId);
                                context.startActivity(intent);
                            }
                        }
                    }
                }



            }
        } else if (JumpType == 2 && !TextUtils.isEmpty(url)) {//2 内嵌页面链接跳转，
            Intent intent = new Intent(context, H5AutoTitleActivity.class);
            intent.putExtra("title", "爱订房");
            intent.putExtra("url", url);
            context.startActivity(intent);
        } else if (JumpType == 3) {//3  跳转小程序",
            wxOpenApplet(context,MiniAppId, url, MiniProgramType);
        }
    }

    private static String matchUrl(String url, String parm) {
        if (url.contains("?")) {
            String parentValue = url.split("\\?")[1];
            if (parentValue.contains(parm)) {
                if (parentValue.contains("&")) {
                    String[] childValue = parentValue.split("&");
                    for (int i = 0; i < childValue.length; i++) {
                        if (childValue[i].contains(parm)) {
                            return childValue[i].split("=")[1];
                        }
                    }
                } else {
                    return parentValue.split("=")[1];
                }
            } else {
                return "";
            }
        } else {
            return "";
        }
        return "";
    }

    /**
     * APP打开小程序
     */
    public static void wxOpenApplet(Context context,String userName, String path, int miniProgramType) {
        IWXAPI api = WXAPIFactory.createWXAPI(context, Constant.ID.WeChat_APP_ID);
        if (!api.isWXAppInstalled()) {//未安装微信
            ToastUtil.initToast(context, "您未安装微信");
            return;

        }
        WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
        req.userName = userName; // 填小程序原始id
        req.path = path;                  //拉起小程序页面的可带参路径，不填默认拉起小程序首页
        //      "miniProgramType": "integer,小程序版本，1：开发版 2：预览版 3：正式版，jumpType为3时必然存在"
        if (miniProgramType == 1) {
            req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_TEST;// 可选打开 开发版，体验版和正式版

        } else if (miniProgramType == 2) {
            req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;// 可选打开 开发版，体验版和正式版

        } else if (miniProgramType == 3) {
            req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版，体验版和正式版

        }

        api.sendReq(req);
    }
}
